<?php
   $auth = Auth::getAuth('current_user');
   $userCompany = new userQueries();
   $company = $userCompany->getCompany($auth['company_id']);
?>

<div class="content_wrapper">
   <div class="padding_5">
      <div class="body_header_container">
         <?php
            // Workspace header
            
            echo functions::workspace_header("Organizational Chart","organizational_chart");
         ?>
      </div>
      <div style="display:table;">
         <div style="display:table-row">
            <div style="display:table-cell"></div>
            <div class="topPointerRuler-container" style="position:relative; display:table-cell;width: auto;height: 100%;border: 1px solid #ddd;border-bottom: none;">
                  <div class="topPointerRuler-tip" style="width: 10px;background-color: #000;height: 10px;cursor: pointer;"></div>
            </div>
         </div>
         <div style="display:table-row">
            <div  class="leftPointerRuler-container" style="position:relative;display:table-cell;white-space:nowrap;vertical-align:top;width: 10px;height: 600px;border: 1px solid #ddd;">
                  <div class="leftPointerRuler-tip" style="width: 10px;background-color: #000;height: 10px;cursor: pointer;"></div>
            </div>
            <div style="display:table-cell">
              <div class="workspace orgchart_ws" style="position:relative">
                  <!-- Node -->
               </div>
            </div>
         </div>
      </div>
      
      <?php
      $db = new Database();
      //$orgchart_id = functions::getMethod_replace($_GET['id'],"-","+");
      
      if(isset($_GET['id'])){
         $orgchart_id = $_GET['id'];
         //Get orgchart information and nodes query
         $orgchart = $db->query("SELECT * FROM tborgchart tbo WHERE tbo.id = {$db->escape($orgchart_id)} and tbo.is_active = 1","array");
         $orgchart_nodes_array = $db->query("SELECT *, tbo.json as tbo_json FROM tborgchart tbo LEFT JOIN tborgchartobjects tboo
                                     on tbo.id = tboo.orgChart_id 
                                     WHERE tbo.id = {$db->escape($orgchart_id)} and tbo.is_active = 1","array");
         if(count($orgchart)!=1){
            header("location:/");
         }
         if($auth['user_level_id']=="3" && $orgchart[0]['status']!=1){
             header("location:/");
         }
         // if($orgchart[0]['']){
         //    // header("location:/");
         // }
         $ws_size = json_decode($orgchart_nodes_array[0]['tbo_json'],true);
         $ws_height = $ws_size['height'];
         $ws_width = $ws_size['width'];
         //node array
         $nodes_json = array();
         //extract nodes and add to array
         foreach($orgchart_nodes_array as $orgchart_nodes){
            array_push($nodes_json,$orgchart_nodes['json']);
         }
         //convert array to json array
         $nodes_json =  htmlspecialchars(json_encode($nodes_json));
         
         //Get orgchart lines query
         $orgchart_lines_array = $db->query("SELECT * FROM tborgchartline 
                                     WHERE orgChart_id = {$db->escape($orgchart_id)}","array");
         $orchart_numrows = $db->query("SELECT * FROM tborgchartline 
                                     WHERE orgChart_id = {$db->escape($orgchart_id)}","numrows");
         if($orchart_numrows>0){
            //echo "1";
         }
         //lines array
         $lines_json = array();
         //extract lines and add to array
         foreach($orgchart_lines_array as $orgchart_lines){
            array_push($lines_json,$orgchart_lines['json']);
         }
         $lines_json = json_encode($lines_json);
            
      }
      ?>
      <input type="text" class="userLevel display2" value="<?php echo $auth['user_level_id']; ?>">
      <div id="orgchart_nodes" class="display"><?php echo $nodes_json; ?></div>
      <div id="orgchart_lines" class="display"><?php echo $lines_json; ?></div>
      <div id="orgchart_height" class="display"><?php echo $ws_height; ?></div>
      <div id="orgchart_width" class="display"><?php echo $ws_width; ?></div>
   </div>
   
</div>